package com.cmf.myproject.service.problemOnline;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cmf.myproject.dal.mybatis.mapper.ProblemOnlineMapper;
import com.cmf.myproject.dal.mybatis.model.ProblemOnline;
import com.cmf.myproject.dal.mybatis.model.ProblemOnlineExample;
import com.cmf.myproject.dal.mybatis.model.ProblemOnlineExample.Criteria;
import com.cmf.myproject.service.problemOnline.dto.ProblemOnlineAddItemReqDto;
import com.cmf.myproject.service.problemOnline.dto.ProblemOnlineQueryFilter;
import com.cmf.myproject.service.problemOnline.dto.ProblemOnlineUpdateItemReqDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.system.core.results.ResultBase;
import com.system.core.view.layui.table.TableViewRspDto;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ProblemOnlineService {
	
	private SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private ProblemOnlineMapper problemOnlineMapper;
	
	/**
	 * 页面列表查询
	 * @return TableViewRspDto 返回TableViewRspDto视图对象，符合Layui规范
	 */
	public TableViewRspDto<ProblemOnline> problemOnlineList(ProblemOnlineQueryFilter filter){
		log.debug("[problemOnline]查询入参：{}",filter);
		TableViewRspDto<ProblemOnline> pageResult = new TableViewRspDto<ProblemOnline>();
		ProblemOnlineExample example = new ProblemOnlineExample();
		Criteria criteria = example.createCriteria();
		//在此插入查询条件start
		if(filter.getId() != null) {
			criteria.andIdEqualTo(filter.getId());
		}
		if(StringUtils.isNotBlank(filter.getProblem())) {
			criteria.andProblemEqualTo(filter.getProblem());
		}
		if(StringUtils.isNotBlank(filter.getReason())) {
			criteria.andReasonEqualTo(filter.getReason());
		}
		if(StringUtils.isNotBlank(filter.getFixMan())) {
			criteria.andFixManEqualTo(filter.getFixMan());
		}
		if(StringUtils.isNotBlank(filter.getSolutionWay())) {
			criteria.andSolutionWayEqualTo(filter.getSolutionWay());
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getStartTimeStrStart())) {
				criteria.andStartTimeGreaterThanOrEqualTo(timeformat.parse(filter.getStartTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getStartTimeStrEnd())) {
				criteria.andStartTimeLessThanOrEqualTo(timeformat.parse(filter.getStartTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getEndTimeStrStart())) {
				criteria.andEndTimeGreaterThanOrEqualTo(timeformat.parse(filter.getEndTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getEndTimeStrEnd())) {
				criteria.andEndTimeLessThanOrEqualTo(timeformat.parse(filter.getEndTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		if(StringUtils.isNotBlank(filter.getPlantform())) {
			criteria.andPlantformEqualTo(filter.getPlantform());
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getCreateTimeStrStart())) {
				criteria.andCreateTimeGreaterThanOrEqualTo(timeformat.parse(filter.getCreateTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getCreateTimeStrEnd())) {
				criteria.andCreateTimeLessThanOrEqualTo(timeformat.parse(filter.getCreateTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getUpdateTimeStrStart())) {
				criteria.andUpdateTimeGreaterThanOrEqualTo(timeformat.parse(filter.getUpdateTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getUpdateTimeStrEnd())) {
				criteria.andUpdateTimeLessThanOrEqualTo(timeformat.parse(filter.getUpdateTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		//在此插入查询条件end
		
		Page<ProblemOnline> page = PageHelper.startPage(filter.getPage(), filter.getLimit());
		example.setOrderByClause("create_time desc");
		pageResult.setData(problemOnlineMapper.selectByExample(example));
		pageResult.setCount(page.getTotal());
		pageResult.setCode(0);
		pageResult.setMessage("查询成功");
		return pageResult;
	}
	
	/**
	 * 根据主键获取单条数据
	 * @param problemOnline 使用problemOnline对象的主键属性Id类型是Long
	 */
	public ProblemOnline problemOnlineGetByPrimaryKey(ProblemOnline problemOnline) {
		ProblemOnline result = problemOnlineMapper.selectByPrimaryKey(problemOnline.getId());
		return result;
	}
	
	/**
	 * 根据主键更新数据
	 * @param problemOnline 被更新对象（请注意，只更新有值属性）
	 */
	public ResultBase problemOnlineUpdateByPrimaryKey(ProblemOnlineUpdateItemReqDto problemOnlineUpdateItemReqDto) {
		ResultBase resultBase = new ResultBase();
		ProblemOnline problemOnline = new ProblemOnline();
		BeanUtils.copyProperties(problemOnlineUpdateItemReqDto, problemOnline);
		problemOnlineMapper.updateByPrimaryKeySelective(problemOnline);
		resultBase.setSuccess(true);
		resultBase.setMessage("更新成功");
		return resultBase;
	}
	
	/**
	 * 根据主键删除数据
	 * @param keys 被删除对象主键List集合
	 */
	@Transactional
	public ResultBase problemOnlineDeleteByPrimaryKey(List<Long> keys) {
		ResultBase resultBase = new ResultBase();
		try {
			keys.forEach((key)->{
				problemOnlineMapper.deleteByPrimaryKey(key);
			});
		} catch (Exception e) {
			resultBase.setMessage(e.getMessage());
			return resultBase;
		}
		resultBase.setSuccess(true);
		resultBase.setMessage("成功删除"+keys.size()+"条记录");
		return resultBase;
	}
	
	/**
	 * 新增数据
	 * @param problemOnlineAddItemReqDto 新增对象
	 */
	@Transactional
	public ResultBase problemOnlineSave(ProblemOnlineAddItemReqDto problemOnlineAddItemReqDto) {
		ResultBase resultBase = new ResultBase();
		ProblemOnline problemOnline = new ProblemOnline();
		BeanUtils.copyProperties(problemOnlineAddItemReqDto, problemOnline);
		problemOnlineMapper.insertSelective(problemOnline);
		resultBase.setSuccess(true);
		resultBase.setMessage("新增成功");
		return resultBase;
	}
}
